﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Qreed.Diagnostics
{
    public class WinFormsTraceListener : TraceListener
    {
        private Control _control;

        public WinFormsTraceListener(Control control)
        {
            _control = control;
        }

        public event EventHandler<TraceListenerWriteEventArgs> WriteEvent;

        public override void Write(string message)
        {
            TraceListenerWriteEventArgs e = new TraceListenerWriteEventArgs();
            e.Message = message;

            if (_control.InvokeRequired)
            {
                _control.BeginInvoke(WriteEvent, new object[] { this, e });
            }
            else
            {
                WriteEvent(this, e);
            }

        }

        public override void WriteLine(string message)
        {
            this.Write(message + System.Environment.NewLine);
        }

        
    }
}
